﻿CREATE Procedure s_MenuIDEx
	@LoanID int,
	@MenuID sysname = null,
	@System bit = 0
As

IF @MenuID IS NULL
BEGIN
	SELECT @MenuID = StatusID FROM t_Loan INNER JOIN fs_Loans_Read(@LoanID,@MenuID) fs_Loans ON t_Loan.ID = fs_Loans.LoanID
	RETURN ISNULL(@MenuID,0)
END

RAISERROR('Don''t go here.',16,1)
/*
IF ISNUMERIC(@MenuID) = 1 AND ABS(@MenuID) = 1 -- Send forward/back (@MenuID contains direction [1,-1])
BEGIN
	RAISERROR('Inappropriate use of s_MenuIDEx.
Report to director''s office at once.',16,1)
	DECLARE @MID int,@CI int
	IF @System = 1 EXEC @CI = sp__Context_WorkFlow_System @System
	SELECT @MID = NextMenuID FROM dbo.NextMenusByLoanID(@LoanID,@MenuID)
	IF @@ROWCOUNT > 1 BEGIN IF @@TRANCOUNT > 0 ROLLBACK TRAN RAISERROR ('Loan ID:%d has multiple menu children.', 16, 1,@LoanID) RETURN END
	IF @MID IS NULL BEGIN
		SELECT  @MenuID = m.MenuPathFull FROM t_Loan l INNER JOIN v_Menu_10 m ON l.StatusID = m.MenuID WHERE l.ID = @LoanID
		IF @@TRANCOUNT > 0 ROLLBACK TRAN
		RAISERROR ('No Next Menu ID for Applicant [%d] in [%s].', 16, 1,@LoanID,@MenuID)
		RETURN
	END
	SET @MenuID = @MID
	IF @System = 1 EXEC sp__Context @CI
END

IF ISNUMERIC(@MenuID) = 0 BEGIN
	SET @MID = dbo.MenuIDByLable(@MenuID)
	IF @MID IS NULL BEGIN IF @@TRANCOUNT > 0 ROLLBACK TRAN RAISERROR ('No Menu ID for [%s].', 16, 1,@MenuID) RETURN END
	SET @MenuID = @MID
END

IF @MenuID > 0 UPDATE t_Loan SET MenuID = @MenuID WHERE ID = @LoanID
ELSE BEGIN IF @@TRANCOUNT > 0 ROLLBACK TRAN RAISERROR ('Menu ID [%s] mast be > 0.', 16, 1,@MenuID) RETURN END

RETURN ISNULL(@MenuID,0)
*/







